{extend name="public/base" /}
{block name="main"}
        <div>API：{$apiInfo.apiName}({$apiInfo.hash})</div>
        <div>
            调用地址：{$domain}/api/{$apiInfo.hash}
        </div>
        <div>
            <form>
                {if condition="$apiInfo.needLogin == 1"}
                <div style="width:100%;">
                    用户SESSION：<input type="text" value="" name="SESSIONID" id="SESSIONID"/>
                </div>
                {/if}
                参数列表：
                
                <div style="width: 100%;">
                {volist name="fieldList" id="field"}
                <li>
                    {$field.fieldName}:{if condition="isset($field.isMust) && $field.isMust == 1"}*{/if}<input id="field_{$field.id}" name="{$field.fieldName}" value="{$field.default}"/>
                </li>
                <span style="padding-left:50px;">
                    {$field.info}
                </span>
                {/volist}
                </div>
                <input type="button" onclick="apiTest();" value="测试接口"/><input type="reset"/>
            </form>
        </div>
        <span>
            调用结果：
        </span>
        <div style="background: #999;" id="result">
            
            skdfjksdjfk
            dsfadf
        </div>
{/block} 
{block name="myScript"}
        <script>
            function apiTest(){
                {volist name="fieldList" id="field"}
                var js{$field.id} = $("#field_{$field.id}").val();
                {if condition="isset($field.isMust) && $field.isMust == 1"}
                if(typeof js{$field.id} == 'undefined' || js{$field.id} ==""){
                    alert("字段{$field.fieldName}不能为空，请填值");
                    return;
                }
                {/if}
                {/volist}
                var data = '';
                
                {volist name="fieldList" id="field"}
                data += '{$field.fieldName}='+js{$field.id}+'&';
                {/volist}
                
                {if condition="$apiInfo.needLogin == 1"}
                var sessionId = $('#SESSIONID').val();
                if(typeof sessionId == "undefined" || sessionId == "" ){
                    alert("当前接口需要登录，请输入用户SESSIONID,此值能过登录接口返回。");
                    return;
                }
                {/if}
                //AJAX获取签名密钥。
                $.ajax({
                    url:'{:url("apidocs/signparams")}',
                    type:'post',
                    data:data + 'app_id={$appInfo.app_id}',
                    dataType:'json',
                    success:function(ret){
                        if(ret.code == 200){
                            $.ajax({
                                url:'/api/{$apiInfo.hash}',
                                
                                headers:{
                                    {if condition="$apiInfo.needLogin == 1"}
                                    'SESSION-ID':sessionId,
                                    {/if}
                                   'ACCESS-TOKEN':ret.data.sign
                                },
                                
                                type:'post',
                                data:data,
                                dataType:'json',
                                success:function(retString){
                                    var resStr = '';
                                    resStr += '请求成功<br/>';
                                    resStr += '参数：' + data + '<br/>';
                                    resStr += '签名：' + ret.data.sign + '<br/>';
                                    resStr += '返回内容：'+'<br/>';
                                    resStr += formatJson(retString);
                                    $('#result').html(resStr);
                               
                                },
                                error:function(XMLHttpRequest, textStatus, errorThrown){
                                    var resStr = '';
                                    resStr += '请求发生错误<br/>';
                                    resStr += '服务器响应：'+XMLHttpRequest.status+'<br/>';
                                    resStr += '参数：' + data + '<br/>';
                                    resStr += '签名：' + ret.data.sign + '<br/>';
                                    resStr += '返回内容：'+'<br/>';
                                    resStr += formatJson(XMLHttpRequest.responseJSON);
                                    $('#result').html(resStr);
                                }
                            });
                        }else{
                            alert(ret.msg);
                        }
                        
                    },
                    error:function(){
                        alert('access error');
                    }
                })
                //alert($(data).html());
                    
            }
    function repeat(s, count) {
        return new Array(count + 1).join(s);
    }

    function formatJson(json) {
        if(typeof json == 'object'){
            json = JSON.stringify(json);
        }
        
        var i           = 0,
            len          = 0,
            tab         = "&nbsp;&nbsp;",
            targetJson     = "",
            indentLevel = 0,
            inString    = false,
            currentChar = null;
            br = '<br/>';


        for (i = 0, len = json.length; i < len; i += 1) { 
            currentChar = json.charAt(i);

            switch (currentChar) {
            case '{': 
            case '[': 
                if (!inString) { 
                    targetJson += currentChar + br + repeat(tab, indentLevel + 1);
                    indentLevel += 1; 
                } else { 
                    targetJson += currentChar; 
                }
                break; 
            case '}': 
            case ']': 
                if (!inString) { 
                    indentLevel -= 1; 
                    targetJson += br + repeat(tab, indentLevel) + currentChar; 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ',': 
                if (!inString) { 
                    targetJson += br + repeat(tab, indentLevel); 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ':': 
                if (!inString) { 
                    targetJson += ": "; 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ' ':
            case "\n":
            case "\t":
                if (inString) {
                    targetJson += currentChar;
                }
                break;
            case '"': 
                if (i > 0 && json.charAt(i - 1) !== '\\') {
                    inString = !inString; 
                }
                targetJson += currentChar; 
                break;
            default: 
                targetJson += currentChar; 
                break;                    
            } 
        } 
        return targetJson;
    }
        </script>
{/block}